﻿using System;
using Model;
using Service;
using Resources;

public partial class sparepartDetail : System.Web.UI.Page
{
    StockService _StockService  = new StockService();
    ReferenceDataService _ReferenceService = new ReferenceDataService();
    Sparepart sparepart = new Sparepart();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            //if (!String.IsNullOrEmpty(Request.QueryString["informationId"]))
            //{
            //    lblCodeNumber.Text = "Spare-part Nº : " + Int32.Parse(Request.QueryString["informationId"]);
            //    LoadData(Int32.Parse(Request.QueryString["informationId"]));
            //}
            int sparepartId = GetSparepartId();

            pnlBody.Visible = sparepartId == 0 ? false : true;
            if (sparepartId == 0) return;


            if(sparepartId !=0)
            {
                lblCodeNumber.Text = "Spare-part Nº : " + sparepartId.ToString();
                LoadData(sparepartId);
            }

            if (Request.QueryString["displayMode"] != null)
            {
                if (Request.QueryString["displayMode"].Equals("true")) btnValidate.Visible = false;
                else btnValidate.Visible = true;
            }else btnValidate.Visible = true;
        }
    }
    public int GetSparepartId()
    {
        //if (!String.IsNullOrEmpty(Request.QueryString["workOrderId"])) return Int32.Parse(Request.QueryString["workOrderId"]);
        if (Session["informationId"] != null) return Int32.Parse(Session["informationId"].ToString());
        return 0;
    }
    
    
    private void LoadData(int informationId)
    {
        SparepartPurchaseInfo  sparepartPurchaseInfo = new SparepartPurchaseInfo();
        sparepart = _StockService.GetSparepart(informationId);
        sparepartPurchaseInfo = _StockService.GetPurchaseInformation(informationId);

        // get of equipment type equipmentTyeid in table [in]
        //select all model coresponding to equipmentypeid   
        EquipmentTypeSystem equipmentTypeSystem = _ReferenceService.GetEquipmentTypeSystem(sparepartPurchaseInfo.HaveId);
        drlModel.DataSource = new ReferenceDataDirectService().GetModelByTypeEquipmentId(equipmentTypeSystem.EquipmentTypeId);
        drlModel.DataValueField = EquipmentModel.CONST_MODEL_ID;
        drlModel.DataTextField = EquipmentModel.CONST_MODEL_LABEL_T;
        drlModel.DataBind();


        drlModel.SelectedValue = sparepart.ModelId.ToString();
        drlSupplier.SelectedValue = sparepartPurchaseInfo.CompanyId.ToString();
        txtCapacity.Text = sparepartPurchaseInfo.Capacity.ToString();
        txtContractDescription.Text = sparepartPurchaseInfo.WarrantyDesc;
       
        txtPurchaseDate.Text = sparepartPurchaseInfo.PurchaseDate.ToString(Resources.Resource.DateEnglishFormat);
        txtPurchaseDescription.Text = sparepartPurchaseInfo.SparepartPurchaseInfoDesc;
        txtQuantity.Text = sparepartPurchaseInfo.Quantity.ToString();
        txtStartDate.Text = sparepartPurchaseInfo.StartDate.ToString(Resources.Resource.DateEnglishFormat);
        txtUnitPrice.Text = sparepartPurchaseInfo.UnitPrice.ToString("N2");
        txtEndDate.Text = sparepartPurchaseInfo.EndDate.ToString(Resources.Resource.DateEnglishFormat);
        chkDiscontinue.Checked = sparepartPurchaseInfo.Discontinue;
    
    }
    protected void btnValidate_Click(object sender, EventArgs e)
    {
        if (!new Utility().TestPermissions(Resource.PermissionValidateSparepart))
        {
            lblMessageValidate.Text = Resource.Permission;
            Page_Load(null, null);
            return;
        }
        else
        {

        
        if (!ValidateData()) return;
        //int sparepartId = !String.IsNullOrEmpty(Request.QueryString["informationId"])
        //                      ?
        //                          Int32.Parse(Request.QueryString["informationId"])
        //                      : 0;
        int sparepartId = GetSparepartId();
        if (sparepartId != 0)
        {
            SparepartPurchaseInfo sparepartInformation = GetSparepartPurchaseInfo(sparepartId);
            sparepart = _StockService.GetSparepart(sparepartId);
            sparepart.ModelId = Int32.Parse(drlModel.SelectedValue);

            _StockService.UpdatePurchaseInformation(sparepartInformation);
            _StockService.UpdateSparepart(sparepart);
            lblInformation.Text = "Information  has been updated successfully.";
        }
        else
        {
            lblInformation.Text = "Information  hasn't been updated. Contact admminstrator for help !";
        }
      }
    }
    private bool ValidateData()
    {
        if (drlModel.SelectedIndex == 0)
        {
            lblRequiredModel.Text = Resources.Resource.MsgFillInformation;
            lblInformation.Text = string.Empty;
            return false;
        }
        lblRequiredModel.Text = string.Empty;

        if(drlSupplier.SelectedIndex == 0)
        {
            lblRequiredSupplier.Text = Resources.Resource.MsgFillInformation;
            lblInformation.Text = string.Empty;
            return false;
        }
        lblRequiredSupplier.Text = string.Empty;
        return true;
     }

    private SparepartPurchaseInfo GetSparepartPurchaseInfo(int informationId)
    {
        SparepartPurchaseInfo sparepartPurchaseInfo  = new SparepartPurchaseInfo();
        sparepartPurchaseInfo.SparepartPurchaseInfoId = informationId;
        sparepartPurchaseInfo = _StockService.GetPurchaseInformation(informationId);
        sparepartPurchaseInfo.CompanyId =  Int32.Parse(drlSupplier.SelectedValue) ;
        sparepartPurchaseInfo.Capacity = float.Parse(txtCapacity.Text);
        sparepartPurchaseInfo.WarrantyDesc = txtContractDescription.Text;

        sparepartPurchaseInfo.PurchaseDate = new Utility().ConvertToCustomizedFormat(txtPurchaseDate.Text);
        sparepartPurchaseInfo.SparepartPurchaseInfoDesc =   txtPurchaseDescription.Text ;
        sparepartPurchaseInfo.Quantity  = !String.IsNullOrEmpty(txtQuantity.Text) ? Int32.Parse(txtQuantity.Text) :0;

        sparepartPurchaseInfo.StartDate = new Utility().ConvertToCustomizedFormat(txtStartDate.Text);

        sparepartPurchaseInfo.UnitPrice = !String.IsNullOrEmpty(txtUnitPrice.Text) ? float.Parse(txtUnitPrice.Text) : 0;
        sparepartPurchaseInfo.EndDate = new Utility().ConvertToCustomizedFormat(txtEndDate.Text);
        sparepartPurchaseInfo.Discontinue = chkDiscontinue.Checked ;
        return sparepartPurchaseInfo;
    }



    protected void drlModel_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
